import sys
import os
import pygame
import Numeric
import random
import re

re_dict = re.compile('.+?(\{.+?\}).+?')

class Video():
        def __init__(self,tp):
                pygame.init()
                pygame.event.set_allowed(pygame.MOUSEBUTTONDOWN)
                self.size = tp
                self._ventana = pygame.display.set_mode(tp,0,32)
                self.array = pygame.surfarray.array2d(self._ventana)

        def refresh(self):
                pygame.surfarray.blit_array(self._ventana, self.array)
                pygame.display.flip()
                return

        def refreshPalette(self):
                self.applyPalette()
                self.refresh()
                return
        
        def pintar(self,color):
                for x in range(self.size[0]):
                        for y in range(self.size[1]):
                                self.array[(x,y)]= int('ff0000',16)
                                self.array[(x,y)]= self.array[(x,y)] + int(color,16)
                self.refresh()
                return
        def applyPalette(self):
                pass                

        def finalizar(self):
                pygame.quit()
                return

        def waitForClick(self):
                clikeo = False
                mouse = pygame.MOUSEBUTTONDOWN
                while not clikeo:
                        if pygame.event.peek(mouse):
                                evs = pygame.event.get(mouse)
                                for evento in evs:
                                        dicc = eval(re_dict.findall(str(evento))[0])
                                        x = dicc['pos'][0]
                                        y = dicc['pos'][1]
                                clikeo = True
                pygame.event.clear()
                return (x,y)
        
                                        
